package com.haoma.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoma.constant.RabbitConstant;
import com.haoma.dto.LinkAuditDto;
import com.haoma.dto.LinkQueryDto;
import com.haoma.entity.Link;
import com.haoma.exception.BlogException;
import com.haoma.mapper.LinkMapper;
import com.haoma.rabbitmq.RabbitService;
import com.haoma.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Description:
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Autowired
    private LinkMapper linkMapper;

    /**
     * 友链分页查询
     *
     * @param page
     * @param limit
     * @param linkQueryDto
     * @return
     */
    public IPage pageLinkList(Long page, Long limit, LinkQueryDto linkQueryDto) {
        // 设置分页参数
        Page<Link> linkPage = new Page<>(page, limit);
        // 设置分页条件
        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 网站名称
        if (linkQueryDto.getName() != null && StringUtils.hasText(linkQueryDto.getName())) {
            linkLambdaQueryWrapper.like(Link::getName, linkQueryDto.getName());
        }
        // 网站状态
        if (linkQueryDto.getStatus() != null) {
            linkLambdaQueryWrapper.eq(Link::getStatus, linkQueryDto.getStatus());
        }
        // 排序字段
        linkLambdaQueryWrapper.orderByDesc(Link::getUpdateTime);
        // 开始分页查询
        return linkMapper.selectPage(linkPage, linkLambdaQueryWrapper);
    }

    /**
     * 删除友链
     *
     * @param id
     */
    public void delLink(Integer id) {
        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(Link::getId, id);
        try {
            linkMapper.delete(linkLambdaQueryWrapper);
        } catch (RuntimeException runtimeException) {
            throw new BlogException("删除失败");
        }
    }

    @Autowired
    private RabbitService rabbitService;


    /**
     * 审核友链
     *
     * @param status
     * @param linkAuditDto
     */
    public void auditLink(Integer status, LinkAuditDto linkAuditDto) {
        // 判断
        if (!StringUtils.hasText(linkAuditDto.getLinkAddress())) {
            throw new BlogException("审核失败");
        }
        // 开始查询
        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(Link::getLinkAddress, linkAuditDto.getLinkAddress());
        Link link = linkMapper.selectOne(linkLambdaQueryWrapper);
        // 判断
        if (link == null) {
            throw new BlogException("审核失败");
        }
        // 修改友链表的状态
        link.setStatus(status);
        // 修改数据库
        linkMapper.update(link, linkLambdaQueryWrapper);
        // 判断状态
        // 生产消息
        if (status == 1) {
            // 审核成功
            // 修改友链申请表状态为成功发送mq消息
            rabbitService.sendMessage(RabbitConstant.EXCHANGE_LINK_AUDIT, RabbitConstant.ROUTING_LINK_SUCCESS, JSONUtil.toJsonStr(linkAuditDto));
        } else {
            // 审核失败
            // 修改友链申请表状态为失败发送mq消息
            rabbitService.sendMessage(RabbitConstant.EXCHANGE_LINK_AUDIT, RabbitConstant.ROUTING_LINK_FAIL, JSONUtil.toJsonStr(linkAuditDto));
        }
    }

    /**
     * 查询待处理友情链接
     *
     * @return
     */
    public List<Link> getPendingLink() {
        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(Link::getStatus, 3);
        linkLambdaQueryWrapper.orderByDesc(Link::getCreateTime);
        List<Link> links = linkMapper.selectList(linkLambdaQueryWrapper);
        return links;
    }
}
